<?php

class baomingAction extends FirstendAction
{

   public function _initialize() {
        parent::_initialize();
        $this->_cate_mod = D('items_cate');
       $this->_mod = D('items')->cache(true, 3600);
       C('DATA_CACHE_TIME',C('yh_site_cachetime'));
    }

    public function index()
    {
        $cate_list = D('items_cate')->cate_cache();
        $this->assign('cate_list', $cate_list['p']); //分类
        $type = I('get.type');
        $channel = I('get.channel');
        if ($type) {
            //用户有其他的需要
            if ($channel) {
                $this->assign('channel', 'xy'); //用户有其他的需要
            } else {
                $id = I('get.id');
                $item = $this->_mod->where('id = ' . $id)->field('id,title,pic_url,price,shop_type')->find();
                !$item && $this->_404();
                $this->assign('item', $item); //留言的商品
            }
            $this->assign('type', $type); //是否是其他商品
            $this->_config_seo(array(
                'title' => '留言'
            ));
            $this->display();
        } else {
            $this->_config_seo(array(
                'title' => '卖家报名'
            ));
            $this->display();
        }

    }

    //接收报名和留言
    public function enroll()
    {
        $content = I('post.');
        $check_code = $this->checkcode($content['code'], 'baoming');
        if($check_code !== true){
            $this->ajaxReturn(0, $check_code);
            return ;
        }
        if($this->check_phone($content['person_tel'])){
            $this->ajaxReturn(0, '手机号格式错误');
        }
        $enroll = new enrollModel();
        $content['type'] = isset($content['channel'])&&$content['channel'] ? $content['channel'] : $content['type'];
        if (isset($content['type']) && $content['type']){
            $content['goods_url'] = $content['from'];
        }else{
            $content['num_iid'] = $this->getId($content['goods_url']);
            if(!$content['num_iid']){
                $this->ajaxReturn(0, '商品链接格式不正确');
            }

//            $fromurl=$content['from'];
//            unset($content['from']);
//            $data=array(
//                'fromurl'=>$fromurl,
//                'content'=>json_encode($content),
//                'tqk_uid'=>$this->tqkuid,
//                'time'=>time()
//            );
//            $url = $this->tqkapi.'/saveenroll';
//            $result = $this->_curl($url,$data);
//            $this->ajaxReturn(1, '报名成功，我们将尽快和您取得联系！');
        }
        $enroll->addEnroll($content);
        $this->ajaxReturn(1, '提交成功，我们将尽快和您取得联系！或者您可以添加我们的客服微信');

    }
    
    private function getId($strurl)
    {
        $preg1 = '/id=(\d+)/is';
        preg_match($preg1, $strurl, $allhtml1);
        if (! empty($allhtml1)) {
            return $allhtml1[1];
        }

        $preg2 = '/taobao.com\/i(\d+).htm/is';
        preg_match($preg2, $strurl, $allhtml2);

        if (! empty($allhtml2)) {
            return $allhtml2[1];
        }

        $preg3 = '/itemId=(\d+)/is';
        preg_match($preg3, $strurl, $allhtml3);

        if (! empty($allhtml3)) {
            return $allhtml3[1];
        }
        return false;
    }

    public function jubao()
    {
    	    $cate_list = D('items_cate')->cate_cache();
	$this->assign('cate_list', $cate_list['p']); //分类
			
	 $this->_config_seo(array(
            'title'=>'举报商家'
        )); 
        $this->display();
    }

    public function report()
    {
        $content = I('post.');
        $check_code = $this->checkcode($content['code'], 'jubao');
        if($check_code !== true){
            $this->ajaxReturn(0, $check_code);
            return ;
        }
        if($content['num_iid'] == ''){
            $this->ajaxReturn(0, '举报商品不存在');
        }
        
        if($content['reason'] == ''){
            $this->ajaxReturn(0, '举报内容不能为空');
        }
        
        $fromurl = $content['from'];
        unset($content['from']);
		$data=array(
		'from'=>$fromurl,
		'content'=>json_encode($content),
		'tqk_uid'=>$this->tqkuid,
		'time'=>time()
		);
        $url = $this->tqkapi.'/savereport';
        $result = $this->_curl($url,$data);
        $this->ajaxReturn(1, '举报成功，我们会第一时间进行处理！');
    }

    private function curlPost($url, $data = array())
    {
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        
        return $return;
    }
}